package com.sky.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.base.exception.MyException;
import com.sky.content.dto.BindTeachplanMediaDto;
import com.sky.content.dto.SaveTeachplanDto;
import com.sky.content.dto.TeachplanDto;
import com.sky.content.mapper.TeachplanMapper;
import com.sky.content.mapper.TeachplanMediaMapper;
import com.sky.content.model.po.Teachplan;
import com.sky.content.model.po.TeachplanMedia;
import com.sky.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 尹稳健~
 * @version 1.0
 * @time 2023/6/11
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Resource
    private TeachplanMapper teachplanMapper;

    @Resource
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        // 根据课程id查询课程计划,查询出顶级
        List<Teachplan> teachplans = teachplanMapper.selectList(new LambdaQueryWrapper<Teachplan>()
                .eq(Teachplan::getCourseId, courseId)
                .eq(Teachplan::getParentid, 0L)
                .orderByAsc(Teachplan::getOrderby));
        List<TeachplanDto> collect = teachplans.stream().map(this::buildTeachPlanDto).collect(Collectors.toList());
        return collect;
    }

    TeachplanDto buildTeachPlanDto(Teachplan teachplan) {
        TeachplanDto teachplanDto = new TeachplanDto();
        BeanUtils.copyProperties(teachplan, teachplanDto);
        List<Teachplan> children = teachplanMapper.selectList(new LambdaQueryWrapper<Teachplan>()
                .eq(Teachplan::getParentid, teachplan.getId())
                .orderByAsc(Teachplan::getOrderby));
        if (!children.isEmpty()){
            teachplanDto.setTeachPlanTreeNodes(children.stream()
                    .map(this::buildTeachPlanDto).collect(Collectors.toList()));
        }
        TeachplanMedia teachplanMedia = teachplanMediaMapper.selectOne(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId,teachplan.getId()));
        teachplanDto.setTeachplanMedia(teachplanMedia);
        return teachplanDto;
    }

    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        if (Objects.isNull(teachplanDto.getId())){
            // 添加
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto,teachplan);
            // 设置排序
            Integer count = teachplanMapper.selectCount(new LambdaQueryWrapper<Teachplan>()
                    .eq(Teachplan::getParentid, teachplanDto.getParentid())
                    .eq(Teachplan::getCourseId, teachplanDto.getCourseId()));
            teachplan.setOrderby(count+1);
            teachplanMapper.insert(teachplan);
        }else{
            // 更新
            Long id = teachplanDto.getId();
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            MyException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            MyException.cast("只允许第二级教学计划绑定媒资文件");
        }
        Long courseId = teachplan.getCourseId();
        int delete = teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId, teachplanId));

        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }

}
